from lxml import etree
 
# 示例 XML 数据
xml_data = """
<library>
    <book id="1">
        <title>Python Programming</title>
        <author>John Doe</author>
        <year>2023</year>
    </book>
    <book id="2">
        <title>Machine Learning</title>
        <author>Jane Smith</author>
        <year>2022</year>
    </book>
</library>
"""
 
# 解析 XML 数据
root = etree.fromstring(xml_data)
 
# 查找所有书名
titles = root.xpath("//title")
for title in titles:
    print(f"书名：{title.text}")
 
# 查找特定年份的书籍
books_2023 = root.xpath("//book[year='2023']")
for book in books_2023:
    print(f"2023年的书名：{book.find('title').text}")
 
# 使用属性查找
book_id_1 = root.xpath("//book[@id='1']")
print(f"ID 为 1 的书名：{book_id_1[0].find('title').text}")